Components.utils.import("resource://mldonkeyff/MLDonkeyUtils.js");
Components.utils.import("resource://mldonkeyff/MLDonkeyClientsContainer.js");
Components.utils.import("resource://mldonkeyff/MLDonkeyClient.js");

var mldonkeyff = {
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("mldonkeyff-strings");
    this.prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
    this.passwordManager = Components.classes["@mozilla.org/login-manager;1"].getService(Components.interfaces.nsILoginManager);
  },     
  
  onMenuItemCommand: function(e) {
  	var logins = new Array();
  	MLDonkeyUtils.getPasswordManager().findLogins({}, "*", null, null, logins);
  	
  	var username = "marco";
  	var password = "xxx";
  	var id = MLDonkeyClient.createIdentifier("localhost", 4001, username);

/////////////////////////////////////////////////
//  	var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Components.interfaces.nsILoginInfo, "init");
//  	var authLoginInfo = new nsLoginInfo(id, null, id, username, password, "", "");
//  	MLDonkeyUtils.getPasswordManager().addLogin(authLoginInfo);
  	
/////////////////////////////////////////////////
//  	var logins = MLDonkeyUtils.getPasswordManager().findLogins({}, id, null, id);
//  	for(var i=0; i<logins.length; i++) {
//    	MLDonkeyUtils.getPromptService().alert(window, "Test", logins[i].hostname + " " + logins[i].httpRealm + " " + logins[i].password + " " + logins[i].username);
//  	}
  	
/////////////////////////////////////////////////
//  	var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Components.interfaces.nsILoginInfo, "init");
//  	var authLoginInfo = new nsLoginInfo(id, null, id, username, password, "", "");
//  	MLDonkeyUtils.getPasswordManager().removeLogin(authLoginInfo);
  	
  	var MY_ID = "mldonkeyff@berserk.bounceme.net";
  	var em = Components.classes["@mozilla.org/extensions/manager;1"].getService(Components.interfaces.nsIExtensionManager);
  	var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "mldonkey.sqlite");
  	
  	var storageService = Components.classes["@mozilla.org/storage/service;1"].getService(Components.interfaces.mozIStorageService);
  	var mDBConn = storageService.openDatabase(file);
  	
  	alert(filestring);
  },
  
  onToolbarButtonCommand: function(e) {
    // just reuse the function above.  you can change this, obviously!
    mldonkeyff.onMenuItemCommand(e);
  },

  onToolbarButtonCommand2: function(e) {
  	try {
	    var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
	  
	    var hostname = this.prefs.getCharPref("extensions.mldonkeyff.hostname");
	    var port     = this.prefs .getIntPref("extensions.mldonkeyff.port");
	    var username = this.prefs.getCharPref("extensions.mldonkeyff.username");
	    var password = this.prefs.getCharPref("extensions.mldonkeyff.password");
	
	    var client = new MLDonkeyClient(hostname, port, username, password);
	    
	    client.connect();
  	} catch(err) {
  		alert(err);
  	}
  }
};

window.addEventListener("load", function () { mldonkeyff.onLoad(); }, false);
